﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;

namespace Admin.NET.Application;
/// <summary>
/// 热词服务
/// </summary>
[AllowAnonymous]
[ApiDescriptionSettings(ApplicationConst.GroupNameApi, Order = 100)]
public class car_searchkeyServiceApi : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_searchkey> _rep;
    public car_searchkeyServiceApi(SqlSugarRepository<car_searchkey> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询热词
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_searchkeyOutput>> Page(car_searchkeyInput input)
    {
        var query= _rep.AsQueryable()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.words), u => u.words.Contains(input.words.Trim()))
                    .WhereIF(input.clickCount>0, u => u.clickCount == input.clickCount)

                    .Select<car_searchkeyOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加热词
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_searchkeyInput input)
    {
        var entity = input.Adapt<car_searchkey>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除热词
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_searchkeyInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新热词
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_searchkeyInput input)
    {
        var entity = input.Adapt<car_searchkey>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取热词
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_searchkey> Get([FromQuery] QueryByIdcar_searchkeyInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取热词列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_searchkeyOutput>> List([FromQuery] car_searchkeyInput input)
    {
        return await _rep.AsQueryable().Select<car_searchkeyOutput>().ToListAsync();
    }





}

